package cn.iocoder.yudao.module.promotion.service.prizedraw;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawNumberRecordPageReqVO;
import cn.iocoder.yudao.module.promotion.controller.admin.prizedraw.vo.PrizeDrawNumberRecordSaveReqVO;
import cn.iocoder.yudao.module.promotion.dal.dataobject.prizedraw.PrizeDrawNumberRecordDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.prizedraw.PrizeDrawNumberRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.PRIZE_DRAW_NUMBER_RECORD_NOT_EXISTS;

/**
 * 抽奖次数详细记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PrizeDrawNumberRecordServiceImpl implements PrizeDrawNumberRecordService {

    @Resource
    private PrizeDrawNumberRecordMapper prizeDrawNumberRecordMapper;

    @Override
    public Long createPrizeDrawNumberRecord(PrizeDrawNumberRecordSaveReqVO createReqVO) {
        // 插入
        PrizeDrawNumberRecordDO prizeDrawNumberRecord = BeanUtils.toBean(createReqVO, PrizeDrawNumberRecordDO.class);
        prizeDrawNumberRecordMapper.insert(prizeDrawNumberRecord);
        // 返回
        return prizeDrawNumberRecord.getPrizeDrawNumberRecordId();
    }

    @Override
    public void updatePrizeDrawNumberRecord(PrizeDrawNumberRecordSaveReqVO updateReqVO) {
        // 校验存在
        validatePrizeDrawNumberRecordExists(updateReqVO.getPrizeDrawNumberRecordId());
        // 更新
        PrizeDrawNumberRecordDO updateObj = BeanUtils.toBean(updateReqVO, PrizeDrawNumberRecordDO.class);
        prizeDrawNumberRecordMapper.updateById(updateObj);
    }

    @Override
    public void deletePrizeDrawNumberRecord(Long id) {
        // 校验存在
        validatePrizeDrawNumberRecordExists(id);
        // 删除
        prizeDrawNumberRecordMapper.deleteById(id);
    }

    @Override
    public void deletePrizeDrawNumberRecordListByIds(List<Long> ids) {
        // 校验存在
        validatePrizeDrawNumberRecordExists(ids);
        // 删除
        prizeDrawNumberRecordMapper.deleteByIds(ids);
    }

    private void validatePrizeDrawNumberRecordExists(List<Long> ids) {
        List<PrizeDrawNumberRecordDO> list = prizeDrawNumberRecordMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(PRIZE_DRAW_NUMBER_RECORD_NOT_EXISTS);
        }
    }

    private void validatePrizeDrawNumberRecordExists(Long id) {
        if (prizeDrawNumberRecordMapper.selectById(id) == null) {
            throw exception(PRIZE_DRAW_NUMBER_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public PrizeDrawNumberRecordDO getPrizeDrawNumberRecord(Long id) {
        return prizeDrawNumberRecordMapper.selectById(id);
    }

    @Override
    public PageResult<PrizeDrawNumberRecordDO> getPrizeDrawNumberRecordPage(PrizeDrawNumberRecordPageReqVO pageReqVO) {
        return prizeDrawNumberRecordMapper.selectPage(pageReqVO);
    }

}